﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Sys.WebRes.Publish.Areas.Admin.Models.FilesModel>" %>
<script language="javascript" type="text/javascript">
    $().ready(function () {
        $(".fileRow:even").css({ background: "#ffffee" });
        $("#addFileBtn").click(function () {
            addLine("", "", "");
        });
        $("#addUploadedBtn").click(function () {
            openServerFileWin();
        });
    });

    function addLine(title, path, ext) {
        var count = $("#fileTable")[0].count;
        $("#fileTable")[0].count = Number(count) + 1;
        var content = $(".fileRow:first").html();
        content = content.replace(/\[\d\]/ig, "[" + count + "]");
        content = content.replace(/_\d__/ig, "_" + count + "__");
        content = content.replace(/\(\d\)/ig, "(" + count + ")");
        $("<tr class='fileRow'>" + content + "</tr>").appendTo("#fileTable");

        setLineValue(count, title, path, ext, "", count);

        $(".fileRow:even").css({ background: "#ffffee" });
    }

    function setLineValue(index, title, path, ext, summary, orderNo) {
        if(ext == "") ext = "noext";
//        <%if(Model.ResTypeID == ResType.Picture.ID){ %>
//        $("#Items_" + index + "__Img")[0].src = '<%=Url.RouteUrl("File")%>/' + path + '/small';
//        <%}else{ %>
//        $("#Items_" + index + "__Img")[0].src = '<%=Url.Content("~/Content/fileIcon/")%>' + ext + '.ico';
        //<% }%>
        $("#Items_" + index + "__ID").val("");
        $("#Items_" + index + "__Img")[0].src = path==""?"<%=Url.Action("Pic", "Attachment", new {area="", path=SysConfig.DefaultPicture}) %>" : path+'&t=true';
        $("#Items_" + index + "__Url").val(path);
        $("#Items_" + index + "__Title").val(title);
        $("#Items_" + index + "__Summary").val(summary);
        if (orderNo) {
            $("#Items_" + index + "__OrderNo").val(orderNo);
        }
    }

    function openServerFileWin(index) {
        var frame = $('<iframe id="SelFileWin" src="" frameborder="0" width="100%" height="100%"></iframe>');
        frame[0].src = '<%=Url.RouteUrl("FileSys") + "/" + ResType.Map[Model.ResTypeID].GetUploadFolder()%>';
        frame[0].okHandler = function (files) {
            if (index >= 0) {
                setLineValue(index, files[0][0], files[0][1], files[0][2]);
            } else {
                $(files).each(function () {
                    addLine(this[0], this[1]);
                });
            }
            $(win).window("close");
            win.remove();
        };
        frame[0].cancelHandler = function () {
            $(win).window("close");
            win.remove();
        };
        var win = $('<div></div>');
        frame.appendTo(win);
        win.appendTo(window.parent == null ? window.document : window.parent.document);
        $(win).window({
            title: '选择...',
            width: 575,
            height: 260,
            modal: true,
            minimizable: false,
            maximizable: false,
            shadow: false,
            closable: true
        });
    }
</script>
<tr><td class="editor-label">
    <label for="Contents"><%= ResType.Map[Model.ResTypeID].Name%>一览</label>
    (<a href="javascript:void(0);" id="addFileBtn">+</a>)<br />
    (<a href="javascript:void(0);" id="addUploadedBtn">附件列表</a>)
</td>
<td class="editor-field" style="height:350px; vertical-align:top;">
    <div id="editorPanel">
    <table id="fileTable" width="100%" count="<%=Model.Items.Count %>">
    <%for(int i = 0; i < Model.Items.Count; i++){%>
        <tr class="fileRow">
            <td width="100" align="center">
                <%--<%if (Model.ResTypeID == ResType.Picture.ID){
                      string path = string.IsNullOrEmpty(Model.Items[i].Url) ? "" : "/" + Model.Items[i].Url;%>
                <img id="Items_<%=i %>__Img" src="<%=Url.RouteUrl("File") + path%>/small" width="100" alt="" />
                <%}else{ %>
                <img id="Items_<%=i %>__Img" src="<%=Url.Content(string.Format("~/Content/fileIcon/{0}.ico", string.IsNullOrEmpty(Model.Items[i].Extension)?"noext":Model.Items[i].Extension)) %>" alt="" />
                <% }%>--%>
                <img id="Items_<%=i %>__Img" src="<%=string.IsNullOrEmpty(Model.Items[i].Url)
                                        ? Url.Action("Pic", "Attachment", new {area="", path=SysConfig.DefaultPicture})
                                        :Model.Items[i].Url+"&t=true"%>" width="100" alt="预览"/>
            </td>
            <td width="450">
                <%=Html.HiddenFor(m=>m.Items[i].ID)%>
                <input type="file" id="file1" name="Items[<%=i %>].File" size="50" /><br />
                <%=Html.TextBoxFor(model => model.Items[i].Url, new { @readonly=true, @size=50 })%>
                <label for="Items_<%=i %>__Url" onclick="openServerFileWin(<%=i %>);">(访问路径)</label>
                <a href="javascript:void(0)" class="btn" onclick="openServerFileWin(<%=i %>);">选择附件</a><br />
                <%=Html.TextBoxFor(model=>model.Items[i].Title) %>
                <label for="Items_<%=i %>__Title">(标题)</label><br />
                <%=Html.TextAreaFor(model => model.Items[i].Summary, new { @rows = 2, @cols = 50 })%>
                <label for="Items_<%=i %>__Summary">(说明)</label>
            </td>
            <td>
                <%=Html.TextBoxFor(model => model.Items[i].OrderNo, new {@size=3, value=Model.Items[i].OrderNo})%>
                <label for="Items_<%=i %>__OrderNo">(序号)</label>
            </td>
            <td>
                <%=Html.CheckBoxFor(model=>model.Items[i].DeleteFlag) %>
                <label for="Items_<%=i %>__DeletedFlag">(删除)</label>
            </td>
        </tr>
    <%} %>
    </table>
    </div>
</td>
</tr>
